﻿using System;

using System.Net;
using System.Net.Mail;
using System.Text;

namespace AllInOnePluginBL.Helpers
{
    public class MailSender
    {
        private const string GMAIL_SMTP_ADDRESS = "smtp.gmail.com";
        private const int SMTP_PORT = 587;
        private const string MAIL_SUBJECT = "Plugin report";
        private const string USER_ACCOUNT = "xbmc.all.in.one.plugin@gmail.com";
        private const string USER_NAME = "All In One Plugin Site";
        private const string ACCOUNT_PASSWORD = "xbmcallinoneplugin";
        private const string OFFLINE_SITE_URL = "http://localhost:63335/AllInOnePluginSite/";
        private const string ONLINE_SITE_URL = "http://xbmcplugin.somee.com/Rss/";
        
        private readonly MailAddress _userAddress;

        public MailSender()
        {
            this._userAddress = new MailAddress(USER_ACCOUNT, USER_NAME);
        }

        public bool SendMail(MailAddress recipientAddress, string mailBody)
        {
            try
            {
                var smtpClient = new SmtpClient
                {
                    Host = GMAIL_SMTP_ADDRESS,
                    Port = SMTP_PORT,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(this._userAddress.Address, ACCOUNT_PASSWORD)
                };

                using (var message = new MailMessage(this._userAddress, recipientAddress)
                {
                    Subject = MAIL_SUBJECT,
                    Body = mailBody
                })
                {
                    smtpClient.Send(message);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        
        public bool SendConfirmationMail(long userId, string userName, string userUniqeGuid)
        {
            try
            {
                var recipientAddress = new MailAddress(userName);

                var smtpClient = new SmtpClient
                {
                    Host = GMAIL_SMTP_ADDRESS,
                    Port = SMTP_PORT,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(this._userAddress.Address, ACCOUNT_PASSWORD)
                };

                using (var message = new MailMessage(this._userAddress, recipientAddress)
                {
                    Subject = "All in one plugin Acount Confirmation Mail",
                    Body = GetConfirmationBody(userId.ToString(), userName, userUniqeGuid),
                    IsBodyHtml = true
                })
                {
                    smtpClient.Send(message);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private string GetConfirmationBody(string userId, string userName, string userGuid)
        {
            var builder = new StringBuilder();
            builder.AppendFormat("Hello {0}!.<br/><br/>", userName);
            builder.AppendLine("You signed up at All in One Plugin Site,");
            builder.AppendLine("Your new account is almost ready, but before you can login you need to confirm your email by visitng the link below:<br/>");
            builder.AppendFormat("<a target=\"_blank\" href=\"{0}ConfirmCode.aspx?userid={1}&key={2}\" title=\"Email Confirmation Page\">Email Confirmation Page</a>.<br/><br/>", ONLINE_SITE_URL, userId, userGuid);
            builder.AppendLine("Once you have visited the verification URL, your account will be activated.<br/>");
            builder.AppendLine("If you have any problems or questions, please reply to this email.<br/><br/>");
            builder.AppendLine("Thanks!<br/><br/>");

            builder.AppendLine("All in one plugin staff");

            return builder.ToString();
        }
    }
}